package com.dnjn.happiness.biz.controller.admin;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnjn.happiness.biz.domain.Training;
import com.dnjn.happiness.biz.domain.TrainingRecord;
import com.dnjn.happiness.biz.domain.TrainingRecordVO;
import com.dnjn.happiness.biz.domain.TrainingTags;
import com.dnjn.happiness.biz.domain.TrainingVO;
import com.dnjn.happiness.biz.service.ITrainingRecordService;
import com.dnjn.happiness.biz.service.ITrainingService;
import com.dnjn.happiness.biz.service.ITrainingTagsService;
import com.dnjn.happiness.common.core.constant.SecurityConstants;
import com.dnjn.happiness.common.core.domain.R;
import com.dnjn.happiness.common.core.model.PositionLadderVO;
import com.dnjn.happiness.common.core.model.SysPostVO;
import com.dnjn.happiness.common.core.utils.StringUtils;
import com.dnjn.happiness.common.cloud.controller.BaseController;
import com.dnjn.happiness.common.core.web.domain.AjaxResult;
import com.dnjn.happiness.common.log.annotation.Log;
import com.dnjn.happiness.common.log.enums.BusinessType;
import com.dnjn.happiness.common.security.annotation.InnerAuth;
import com.dnjn.happiness.common.security.annotation.RequiresPermissions;
import com.dnjn.happiness.common.security.utils.SecurityUtils;
import com.dnjn.happiness.common.security.utils.VerifyUtil;
import com.dnjn.happiness.system.api.RemoteDeptService;
import com.dnjn.happiness.system.api.RemotePostService;
import com.dnjn.happiness.system.api.RemoteRoleService;
import com.dnjn.happiness.system.api.RemoteUserService;
import com.dnjn.happiness.system.api.domain.SysDept;
import com.dnjn.happiness.system.api.domain.SysUser;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 发起培训Controller
 *
 * @author amber
 * @date 2023-10-11
 */
@Tag(name = "admin biz training 培训管理", description = "培训")
@RestController
@RequestMapping("/admin/training")
public class AdminTrainingController extends BaseController {
    @Autowired
    private ITrainingService trainingService;

    @Autowired
    private ITrainingRecordService trainingRecordService;

    @Autowired
    private RemoteUserService userService;

    @Autowired
    private RemotePostService postService;

    @Autowired
    private RemoteDeptService deptService;



    @Autowired
    private ITrainingTagsService trainingTagsService;


    @InnerAuth
    @GetMapping("/getTraingRecordNotJoin/{userId}/{time}")
    public R<Integer> getTraingRecordNotJoin(@PathVariable Long userId,@PathVariable String time) {
        return R.ok(trainingService.getTraingRecordNotJoin(userId,time));
    }

    @InnerAuth
    @GetMapping("/getTraingRecordYesJoin/{userId}")
    public R<Integer> getTraingRecordYesJoin(@PathVariable Long userId,@PathVariable String time) {
        return R.ok(trainingService.getTraingRecordYesJoin(userId,time));
    }


    /**
     * 分页查询  后台-管理接口
     */
    @RequiresPermissions("biz:training:list")
    @Operation(summary = "后台-管理接口")
    @PostMapping("/page")
    public AjaxResult list(@RequestBody TrainingRecordVO training, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {

        String trainingContent = training.getTrainingContent();
        String text = training.getTrainingContent();
        Map<String, Object> example = new HashMap<>();
        example.put("dateStart", training.getDateStart());
        example.put("dateEnd", training.getDateEnd());
        example.put("trainingStatus", training.getTrainingStatus());
        example.put("trainingTitle", training.getTrainingTitle());
        example.put("initiator", training.getInitiator());
        if (VerifyUtil.whetherAllPermissions()) {
            example.put("whetherAllPermissions", true);
        } else {
            example.put("whetherAllPermissions", false);
        }
        Page<Training> adviceTagsPage = trainingService.pageCommentVo(current, size, example);
        return success(adviceTagsPage);
    }

    @RequiresPermissions("biz:training:query")
    @Operation(summary = "后台-我的培训,后台菜单管理-个人详情-培训模块用到这个接口了")
    @PostMapping("/listQueryByMy")
    public AjaxResult listQueryByMy(@RequestBody TrainingRecordVO domain, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {

        Page<Training> list = trainingService.listQueryByMy(domain, Page.of(current, size).addOrder(OrderItem.desc("training_id")));
        return success(list);

    }

    @RequiresPermissions("biz:training:query")
    @Operation(summary = "后台-我的培训,后台菜单管理-个人详情-培训模块用到这个接口了-搜索框预备数据")
    @GetMapping("/listQueryTrainingTags")
    public AjaxResult listQueryTrainingTags() {
        List<TrainingTags> list = trainingTagsService.lambdaQuery().eq(TrainingTags::getDeleted, '0').list();
        return success(list);

    }

    /**
     * 获取发起培训详细信息
     */
    @RequiresPermissions("biz:training:query")
    @Operation(summary = "获取发起培训详细信息")
    @GetMapping(value = "/{trainingId}")
    public AjaxResult getInfo(@PathVariable("trainingId") String trainingId) {

        Training one = trainingService.lambdaQuery().eq(Training::getDeleted, "0").eq(Training::getTrainingId, trainingId).one();
        if (null != one) {
            //再根据traingId 去查询记录
            List<TrainingRecord> records = trainingRecordService.lambdaQuery().eq(TrainingRecord::getTrainingId, trainingId).list();
            if (!CollectionUtils.isEmpty(records)) {
                one.setUserIds(records.stream().map(item -> {
                    return item.getUserId();
                }).collect(Collectors.toList()));
            }
            List<TrainingRecord> recordList = new ArrayList<>();
            String checkedUserIds = one.getCheckedUserIds();
            if (!StringUtils.isEmpty(checkedUserIds)) {
                for (String userIdStr : checkedUserIds.split(",")) {
                    Long userId = Long.parseLong(userIdStr);
                    SysUser user = null;
                    R<SysUser> result = userService.selectUserInfo(userId, SecurityConstants.INNER);
                    if (StringUtils.isNotNull(result) && StringUtils.isNotNull(result.getData())) {
                        user = result.getData();
                    }
                    if (null != user) {
                        TrainingRecord trainingRecord = new TrainingRecord();
                        trainingRecord.setUserId(userId);
                        trainingRecord.setNickName(user.getNickName());
                        recordList.add(trainingRecord);
                    }
                }
            }
            one.setRecordList(recordList);

            //回显部门名称,岗位名称,职级名称
            if (!StringUtils.isEmpty(one.getDeptIds())) {
                List<SysDept> deptList = new ArrayList<>();
                String deptIds = one.getDeptIds();
                String[] split = deptIds.split(",");
                for (String deptIdStr : split) {
                    SysUser user = new SysUser();
                    long deptId = Long.parseLong(deptIdStr);
                    user.setDeptId(deptId);
                    List<SysUser> sysUserList = new ArrayList<>();
                    R<List<SysUser>> result = userService.selectUserList(user, SecurityConstants.INNER);
                    if (StringUtils.isNotNull(result) && StringUtils.isNotNull(result.getData())) {
                        sysUserList = result.getData();
                    }
                    SysDept dept = null;
                    R<SysDept> resultDept = deptService.getInfoById(deptId, SecurityConstants.INNER);
                    if (StringUtils.isNotNull(result) && StringUtils.isNotNull(result.getData())) {
                        dept = resultDept.getData();
                    }
                    if (null != dept) {
                        dept.setCount(sysUserList.size());
                        deptList.add(dept);
                    }
                }
                one.setSelectDeptList(deptList);
            }
            if (!StringUtils.isEmpty(one.getPostIds())) {
                List<SysPostVO> postList = new ArrayList<>();
                String[] split = one.getPostIds().split(",");
                for (String postIdStr : split) {
                    Long postId = Long.parseLong(postIdStr);
                    SysPostVO post = null;
                    R<SysPostVO> result = postService.getInfoById(postId, SecurityConstants.INNER);
                    if (StringUtils.isNotNull(result) && StringUtils.isNotNull(result.getData())) {
                        post = result.getData();
                    }
                    if (null != post) {
                        Long deptId = SecurityUtils.getLoginUser().getUser().getDeptId();
                        Map<String, Object> map = new HashMap<>();
                        map.put("postId", postId);
                        map.put("deptId", deptId);
                        R<List<SysUser>> resultUserList = userService.getUsersByPostId(map, SecurityConstants.INNER);
                        if (StringUtils.isNotNull(resultUserList) && StringUtils.isNotNull(resultUserList.getData())) {
                            post.setCount(resultUserList.getData().size());
                        } else {
                            post.setCount(0);
                        }

                        postList.add(post);
                    }
                }
                one.setSelectPostList(postList);
            }
            if (!StringUtils.isEmpty(one.getPositionIds())) {
                List<PositionLadderVO> positionLadderList = new ArrayList<>();
                String[] split = one.getPositionIds().split(",");
                for (String position : split) {
                    PositionLadderVO positionLadder = new PositionLadderVO();
                    positionLadder.setPositionLadder(position);
                    Long deptId = SecurityUtils.getLoginUser().getUser().getDeptId();
                    Map<String, Object> map = new HashMap<>();
                    map.put("position", position);
                    map.put("deptId", deptId);
                    R<List<SysUser>> result = userService.getUsersByPosition(map, SecurityConstants.INNER);
                    if (StringUtils.isNotNull(result) && StringUtils.isNotNull(result.getData())) {
                        positionLadder.setCount(result.getData().size());
                    } else {
                        positionLadder.setCount(0);
                    }
                    positionLadderList.add(positionLadder);
                }
                one.setSelectPosttionList(positionLadderList);
            }
        }

        return success(one);
    }

    /**
     * 删除发起培训
     */
    @RequiresPermissions("biz:training:remove")
    @Operation(summary = "删除发起培训")
    @Log(title = "发起培训", businessType = BusinessType.DELETE)
    @DeleteMapping("/{trainingIds}")
    public AjaxResult remove(@PathVariable String trainingIds) {
        return toAjax(trainingService.lambdaUpdate().eq(Training::getTrainingId, trainingIds).set(Training::getDeleted, "1").update());
    }

    /**
     * 新增发起培训
     */
    @RequiresPermissions("biz:training:add")
    @Operation(summary = "新增发起培训")
    @Log(title = "发起培训", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody TrainingVO training) {
        Long userId = SecurityUtils.getUserId();//拿到当前登录用户用户作为发起者
        training.setInitiator(userId.toString());
        Assert.notNull(training.getDateStart(), "开始时间不能为空");
        Assert.notNull(training.getDateEnd(), "结束时间不能为空");
        Assert.notNull(training.getTrainingTitle(), "培训标题不能为空");
        Assert.notNull(training.getTrainingContent(), "培训内容不能为空");
        Assert.notNull(training.getTrainingTagsId(), "培训类型不能为空");
        String trainingContent = training.getTrainingContent();

        //处理参加人部门列表-》参加人列表
        List<Long> deptIdList = training.getDeptIdList();
        if (!CollectionUtils.isEmpty(deptIdList) && deptIdList.size() > 0) {
            List<Long> userList = new ArrayList<>();
            //根据部门id查询部门下所有用户id
            for (Long deptId : deptIdList) {
                R<List<Long>> result = userService.selectUserListByDeptIdList(deptId, SecurityConstants.INNER);
                if (StringUtils.isNotNull(result) && StringUtils.isNotNull(result.getData())) {
                    userList.addAll(result.getData());
                }
            }
            if (null != userList && userList.size() > 0) {
                training.setUserIds(userList);
            }
            String deptIds = deptIdList.stream().map(String::valueOf).collect(Collectors.joining(","));
            training.setDeptIds(deptIds);
        }
        return toAjax(trainingService.insertTraining(training));
    }

    /**
     * 修改发起培训
     */
    @RequiresPermissions("biz:training:edit")
    @Operation(summary = "修改发起培训")
    @Log(title = "修改培训", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody TrainingVO training) {
        Long userId = SecurityUtils.getUserId();//拿到当前登录用户用户作为发起者
        training.setInitiator(userId.toString());
        Assert.notNull(training.getDateStart(), "开始时间不能为空");
        Assert.notNull(training.getDateEnd(), "结束时间不能为空");
        Assert.notNull(training.getTrainingTitle(), "培训标题不能为空");
        Assert.notNull(training.getTrainingContent(), "培训内容不能为空");
        Assert.notNull(training.getTrainingTagsId(), "培训类型不能为空");



        List<Long> deptIdList = training.getDeptIdList();
        if (!CollectionUtils.isEmpty(deptIdList) && deptIdList.size() > 0) {
            List<Long> userList = new ArrayList<>();
            //根据部门id查询部门下所有用户id
            for (Long deptId : deptIdList) {
                R<List<Long>> result = userService.selectUserListByDeptIdList(deptId, SecurityConstants.INNER);
                if (StringUtils.isNotNull(result) && StringUtils.isNotNull(result.getData())) {
                    userList.addAll(result.getData());
                }
            }
            String deptIds = deptIdList.stream().map(String::valueOf).collect(Collectors.joining(","));
            training.setDeptIds(deptIds);
        } else {
            training.setDeptIds(null);
        }

        return toAjax(trainingService.updateTraining(training));
    }

    /**
     * 获取培训所有反馈记录
     */
    @RequiresPermissions("biz:training:query")
    @Operation(summary = "获取发起培训详细信息")
    @GetMapping(value = "/getResultListByTrainingId/{trainingId}")
    public AjaxResult getResultListByTrainingId(@PathVariable("trainingId") String trainingId) {
        List<TrainingRecord> list = trainingRecordService.getResultListByTrainingId(trainingId);
        return success(list);
    }


    /**
     * 发起培训 | 发起问卷，查询当前用户部门下的所有用户岗位列表 - 后台
     */
    @Operation(summary = "发起培训|问卷,查询当前用户部门下的所有用户岗位列表-后台")
    @RequiresPermissions("biz:training:query")
    @GetMapping("/getAllPostList")
    public AjaxResult getAllPostList() {
        Long deptId = SecurityUtils.getLoginUser().getUser().getDeptId();
        List<SysPostVO> postList = new ArrayList<>();
        R<List<SysPostVO>> result = postService.queryPostListByDeptId(deptId, SecurityConstants.INNER);
        if (StringUtils.isNotNull(result) && StringUtils.isNotNull(result.getData())) {
            postList = result.getData();
        }
        return AjaxResult.success(postList);
    }


    /**
     * 根据postId岗位id查询当前用户部门下该岗位的用户列表 - 后台
     */
    @Operation(summary = "根据postId岗位id查询当前用户下该岗位用户列表-nickName模糊搜索")
    @RequiresPermissions("biz:training:query")
    @PostMapping("/getUsersByPostId")
    public AjaxResult getUsersByPostId(@RequestParam(required = false) Long postId, @RequestParam(required = false) String nickName) {
        List<SysUser> userList = new ArrayList<>();
        if (ObjectUtils.isEmpty(postId)) {
            return AjaxResult.success(userList);
        }
        Long deptId = SecurityUtils.getLoginUser().getUser().getDeptId();
        Map<String, Object> map = new HashMap<>();
        map.put("postId", postId);
        map.put("deptId", deptId);
        map.put("nickName", nickName);
        R<List<SysUser>> resultUserList = userService.getUsersByPostId(map, SecurityConstants.INNER);
        if (StringUtils.isNotNull(resultUserList) && StringUtils.isNotNull(resultUserList.getData())) {
            userList = resultUserList.getData();
        }

        return AjaxResult.success(userList);
    }

    /**
     * 发起培训，发起问卷，查询职级列表 - 后台
     */
    @Operation(summary = "发起培训，发起问卷，查询职级列表")
    @RequiresPermissions("biz:training:query")
    @GetMapping("/getPositionLadderList")
    public AjaxResult getPositionList() {

        Long deptId = SecurityUtils.getLoginUser().getUser().getDeptId();
        List<PositionLadderVO> list = new ArrayList<>();
        R<List<PositionLadderVO>> result = userService.queryPositionListByDeptId(deptId, SecurityConstants.INNER);
        if (StringUtils.isNotNull(result) && StringUtils.isNotNull(result.getData())) {
            list = result.getData();
        }
        return AjaxResult.success(list);
    }

    /**
     * 根据职级查询当前用户部门下该职级的用户列表 - 后台
     */
    @Operation(summary = "根据职级查询当前用户下该职级的用户列表-nickName模糊搜索")
    @RequiresPermissions("biz:training:query")
    @PostMapping("/getUsersByPositionCode")
    public AjaxResult getUsersByPositionCode(@RequestParam(required = false) String position, @RequestParam(required = false) String nickName) {
        List<SysUser> userList = new ArrayList<>();
        if (StringUtils.isEmpty(position)) {
            return AjaxResult.success(userList);
        }
        Long deptId = SecurityUtils.getLoginUser().getUser().getDeptId();
        Map<String, Object> map = new HashMap<>();
        map.put("position", position);
        map.put("deptId", deptId);
        map.put("nickName", nickName);
        R<List<SysUser>> result = userService.getUsersByPosition(map, SecurityConstants.INNER);
        if (StringUtils.isNotNull(result) && StringUtils.isNotNull(result.getData())) {
            userList = result.getData();
        }
        return AjaxResult.success(userList);
    }


    /**
     * 团队 | 发起培训 | 发起问卷 查询 机构结构接口 - 后台
     */
//    @RequiresPermissions("biz:training:query")
    @PostMapping("/getTeamDataByDeptId")
    public AjaxResult getTeamDataByDeptId(@RequestParam(required = false) Long deptId, @RequestParam(required = false) String nickName) {
        AjaxResult ajax = new AjaxResult();
        List<SysDept> deptList = new ArrayList<>();
        List<SysUser> userList = new ArrayList<>();
        if (ObjectUtils.isEmpty(deptId)) {//查询当前顶级部门(parentId=0)和顶级部门下的列表
            deptId = SecurityUtils.getLoginUser().getUser().getDeptId();

        }
        if (StringUtils.isEmpty(nickName)) {
            R<List<SysDept>> result = deptService.getChildDeptListById(deptId, SecurityConstants.INNER);
            if (StringUtils.isNotNull(result) && StringUtils.isNotNull(result.getData())) {
                deptList = result.getData();
            }
        }
        if (deptList.size() > 0) {
            for (SysDept dept : deptList) {

                SysUser user = new SysUser();
                user.setDeptId(dept.getDeptId());

                Integer count = 0;
                R<Integer> result = userService.selectUserCount(dept.getDeptId(), SecurityConstants.INNER);
                if (StringUtils.isNotNull(result) && StringUtils.isNotNull(result.getData())) {
                    count = result.getData();
                }
                dept.setCount(count);
            }
        }
        //查询用户列表 selectUserById
        R<List<SysUser>> result = userService.queryUserListByDeptId(deptId, nickName, SecurityConstants.INNER);
        if (StringUtils.isNotNull(result) && StringUtils.isNotNull(result.getData())) {
            userList = result.getData();
        }
//        setRoleAndPost(userList);
        ajax.put("deptList", deptList);
        ajax.put("userList", userList);
        return AjaxResult.success(ajax);
    }





    /**
     * 新增发起培训
     */
    @RequiresPermissions("biz:training:add")
    @Operation(summary = "新增培训")
    @Log(title = "发起培训", businessType = BusinessType.INSERT)
    @PostMapping("/insertTraining")
    public AjaxResult insertTraining(@RequestBody TrainingVO training) {
        Long userId = SecurityUtils.getUserId();//拿到当前登录用户用户作为发起者
        training.setInitiator(userId.toString());
//        Assert.notNull(training.getInitiator(), "发起者ID不能为空");
        Assert.notNull(training.getDateStart(), "开始时间不能为空");
        Assert.notNull(training.getDateEnd(), "结束时间不能为空");
        Assert.notNull(training.getTrainingTitle(), "培训标题不能为空");
        Assert.notNull(training.getTrainingContent(), "培训内容不能为空");
        Assert.notNull(training.getTrainingTagsId(), "培训类型不能为空");
        String trainingContent = training.getTrainingContent();

        if (!StringUtils.isEmpty(training.getSued())) {
            if ("1".equals(training.getSued())) {
                training.setSued("Y");
            } else {
                training.setSued("N");
            }
        }
        //处理参加人列表
        setTrainingUserIds(training);
        return toAjax(trainingService.insertTraining(training));
    }


    /**
     * 修改发起培训
     */
    @RequiresPermissions("biz:training:edit")
    @Operation(summary = "修改培训")
    @Log(title = "修改培训", businessType = BusinessType.UPDATE)
    @PutMapping("/editTraining")
    public AjaxResult editTraining(@RequestBody TrainingVO training) {
        Long userId = SecurityUtils.getUserId();//拿到当前登录用户用户作为发起者
        training.setInitiator(userId.toString());
        Assert.notNull(training.getDateStart(), "开始时间不能为空");
        Assert.notNull(training.getDateEnd(), "结束时间不能为空");
        Assert.notNull(training.getTrainingTitle(), "培训标题不能为空");
        Assert.notNull(training.getTrainingContent(), "培训内容不能为空");
        Assert.notNull(training.getTrainingTagsId(), "培训类型不能为空");


        if (!StringUtils.isEmpty(training.getSued())) {
            if ("1".equals(training.getSued())) {
                training.setSued("Y");
            } else {
                training.setSued("N");
            }
        }
        //处理参加人列表
        setTrainingUserIds(training);
        return toAjax(trainingService.updateTraining(training));
    }


    private void setTrainingUserIds(TrainingVO training) {
        Set<Long> userIdSet = new HashSet<>();
        if ("dept".equals(training.getType())) {
            String deptIds = training.getDeptIds();
            ;
            if (!StringUtils.isEmpty(deptIds)) {
                List<String> list = new ArrayList<>();
                List<Long> deptIdList = new ArrayList<>();
                String[] str = deptIds.split(",");
                list = Arrays.asList(str);
                list.forEach(item -> {
                    deptIdList.add(Long.parseLong(item));
                });
                //根据部门id查询部门下所有用户id
                for (Long deptId : deptIdList) {
                    R<List<Long>> result = userService.selectUserListByDeptIdList(deptId, SecurityConstants.INNER);
                    if (StringUtils.isNotNull(result) && StringUtils.isNotNull(result.getData())) {
                        userIdSet.addAll(result.getData());
                    }
                }
            }
        }
        if ("post".equals(training.getType())) {
            String postIds = training.getPostIds();
            if (!StringUtils.isEmpty(postIds)) {
                List<String> list = new ArrayList<>();
                List<Long> postIdList = new ArrayList<>();
                String[] str = postIds.split(",");
                list = Arrays.asList(str);
                list.forEach(item -> {
                    postIdList.add(Long.parseLong(item));
                });
                for (Long postId : postIdList) {
                    Long deptId = SecurityUtils.getLoginUser().getUser().getDeptId();
                    Map<String, Object> map = new HashMap<>();
                    map.put("postId", postId);
                    map.put("deptId", deptId);
                    R<List<SysUser>> result = userService.getUsersByPostId(map, SecurityConstants.INNER);
                    if (StringUtils.isNotNull(result) && StringUtils.isNotNull(result.getData())) {
                        userIdSet.addAll(result.getData().stream().map(item -> {
                            return item.getUserId();
                        }).collect(Collectors.toList()));
                    }
                }
            }
        }

        if ("position".equals(training.getType())) {
            String positionIds = training.getPositionIds();
            if (!StringUtils.isEmpty(positionIds)) {
                List<String> list = new ArrayList<>();
                String[] str = positionIds.split(",");
                list = Arrays.asList(str);
                for (String position : list) {
                    Long deptId = SecurityUtils.getLoginUser().getUser().getDeptId();
                    Map<String, Object> map = new HashMap<>();
                    map.put("position", position);
                    map.put("deptId", deptId);
                    R<List<SysUser>> result = userService.getUsersByPosition(map, SecurityConstants.INNER);
                    if (StringUtils.isNotNull(result) && StringUtils.isNotNull(result.getData())) {
                        userIdSet.addAll(result.getData().stream().map(item -> {
                            return item.getUserId();
                        }).collect(Collectors.toList()));
                    }
                }
            }
        }
        List<Long> userIdList = training.getUserIds();
        if (!CollectionUtils.isEmpty(userIdList)) {
            training.setCheckedUserIds(StringUtils.join(userIdList, ","));
            userIdSet.addAll(userIdList);
        } else {
            training.setCheckedUserIds(null);
        }
        List<Long> userIds = new ArrayList<>(userIdSet);
        training.setUserIds(userIds);
    }


    @Operation(summary = "第一版本-统计-培训列表")
    @RequiresPermissions("biz:statistical:list")
    @GetMapping("/selectTraining")
    public AjaxResult TrainingList() {

        List<Training> list = trainingService.lambdaQuery().eq(Training::getDeleted, '0').list();
        return success(list);

    }
}
